package cn.edu.tju.elm.service.Impl;

import cn.edu.tju.elm.model.DeliveryAddress;
import cn.edu.tju.elm.repository.DeliveryAddressRepository;
import cn.edu.tju.elm.service.DeliveryAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class DeliveryAddressServiceImpl implements DeliveryAddressService {

    private final DeliveryAddressRepository deliveryAddressRepository;

    @Autowired
    public DeliveryAddressServiceImpl(DeliveryAddressRepository deliveryAddressRepository) {
        this.deliveryAddressRepository = deliveryAddressRepository;
    }

    @Override
    public Optional<DeliveryAddress> findById(Long id) {
        return deliveryAddressRepository.findById(id);
    }

    @Override
    public Optional<DeliveryAddress> findByIdAndCustomerId(Long id, Long userId) {
        return deliveryAddressRepository.findByIdAndCustomerId(id, userId);
    }

    @Override
    public List<DeliveryAddress> findByCustomerId(Long userId) {
        return deliveryAddressRepository.findByCustomerIdOrderByCreateTimeDesc(userId);
    }

    @Override
    public DeliveryAddress save(DeliveryAddress deliveryAddress) {
        return deliveryAddressRepository.save(deliveryAddress);
    }

    @Override
    public void deleteById(Long id) {
        deliveryAddressRepository.deleteById(id);
    }
}